package com.baba.bracelet.controller;

import com.alibaba.fastjson.JSONObject;
import com.baba.bracelet.base.BaseApiService;
import com.baba.bracelet.base.BaseResponse;
import com.baba.bracelet.client.ServerImplMqttClient;
import com.baba.bracelet.config.PublishConfig;
import com.baba.bracelet.handler.MessageHandler;
import com.baba.bracelet.handler.PublishHandler;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * @author wulongbo
 * @version 1.0
 * @date 2020/6/12 20:03
 * @description
 */
@RestController
@RequestMapping("/downstream")
@CrossOrigin
@Api(tags = "服务器发起（服务器-->设备）")
public class DownController extends BaseApiService<JSONObject> {

    private String IMEI;

    private String cBackTopic;

    @Autowired
    private PublishConfig publishConfig;



    @ApiOperation(value="GPRS定位数据上传时间间隔", notes="GPRS定位数据上传时间间隔")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "imsi", dataType = "String", required = true, value = "设备imsi"),
            @ApiImplicitParam(paramType = "query", name = "space", dataType = "int", required = true, value = "时间间隔(s)")})
    @RequestMapping("/gprsCycle")
    public BaseResponse<JSONObject> gprsCycle(String imsi, Integer space) throws IOException {
        ServerImplMqttClient mqtt=ServerImplMqttClient.getInstance(publishConfig);
        mqtt.connect(new MessageHandler() {

            public void messageArrived(cn.hutool.json.JSONObject receivedMessage) {

            }

            public void onError(Throwable throwable) {

            }

            public void onComplete(IMqttDeliveryToken iMqttDeliveryToken) {

            }

            public void onConnected() {

            }


        });
        /**
         * 判断是否在线
         */
        if (mqtt.isConnected()) {
            /**
             * 封装JSON
             */
            cn.hutool.json.JSONObject jsonObject = new cn.hutool.json.JSONObject();
            publishConfig.setTopic(publishConfig.getDevicePublishTopic(imsi));
            jsonObject.put("imsi",imsi);
            jsonObject.put("space",space);
            publishConfig.setPayload(jsonObject);
            /**
             * 使用封装好的Pub Bean
             */
            mqtt.publish(publishConfig, new PublishHandler() {
                @Override
                public void onSuccess() {
                    System.out.println("Pub Bean 消息发送成功！ Topic:"+publishConfig.getTopic());
                }

                @Override
                public void onException(Exception e) {

                }
            });


        } else {
            System.out.println("连接失败");
        }
        return setResultSuccess();
    }




}
